<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>defineProperty</title>
</head>
<body>
<script>
    function Ninja(){
        let _skillLevel = 0; // 定义私有变量
        Object.defineProperty(this,'skillLevel',{
            get() {
                console.log('get method is called');
                return _skillLevel; // 闭包访问私有变量
            },
            set(v) {
                console.log('set method is called');
                _skillLevel = v;
            }
        })
    }
    const ninja = new Ninja();
    console.log(ninja.skillLevel);
    ninja.skillLevel = 2;
</script>
</body>
</html>
